<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Model;

class Person extends Model
{
	use HasDateTimeFormatter;
    protected $table = 'person';
    protected $appends = ['department_text', 'category_text'];

    public static $department_arr = [
        1 => '售后服务部',
    ];

    public static $category_arr = [
        1 =>  '车辆钳工（准）',
        10 => '车辆钳工',
        2 => '车辆电工（准）',
        4 => '车辆电工',
        3 => '车辆内装工（准）',
        5 => '车辆内装工',
        6 => '车辆调工（准）',
        7 => '车辆调工',
        8 => '车辆装调工',
        9 => '车辆装调工（准）',
    ];

    public function getCategoryAttribute($category)
    {
        return array_values(json_decode($category, true) ?: []);
    }

    public function setCategoryAttribute($category)
    {
        $this->attributes['category'] = json_encode(array_values($category));
    }

    public function getCategoryTextAttribute() {
        $text = '';
        if (empty($this->category)) {
            return $text;
        }

        foreach ($this->category as $item) {
            $text .= self::$category_arr[$item] . ',';
        }
        return substr($text, 0, -1);
    }

    public function getJobAttribute($job)
    {
        return array_values(json_decode($job, true) ?: []);
    }

    public function setJobAttribute($job)
    {
        $this->attributes['job'] = json_encode(array_values($job));
    }

    public function getStationAttribute($station)
    {
        return array_values(json_decode($station, true) ?: []);
    }

    public function setStationAttribute($station)
    {
        $this->attributes['station'] = json_encode(array_values($station));
    }

    public function getDepartmentTextAttribute() {
        return self::$department_arr[$this->department];
    }
}
